package test.stream;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.*;

/**
 * @author yahao.guo
 * @version 1.0
 * @date 2019-04-28 17:13
 */
public class Menu {


    public static void main(String[] args) {
        List<Dish> menu = Arrays.asList(
                new Dish("pork", false, 800, Dish.Type.MEAT),
                new Dish("beef", false, 700, Dish.Type.MEAT),
                new Dish("chicken", false, 400, Dish.Type.MEAT),
                new Dish("french fries", true, 530, Dish.Type.OTHER),
                new Dish("rice", true, 350, Dish.Type.OTHER),
                new Dish("season fruit", true, 120, Dish.Type.OTHER),
                new Dish("pizza", true, 550, Dish.Type.OTHER),
                new Dish("prawns", false, 300, Dish.Type.FISH),
                new Dish("salmon", false, 450, Dish.Type.FISH));
        System.out.println(menu);
        Map<Dish.Type, List<Dish>> map = menu.stream().collect(groupingBy(Dish::getType));
        System.out.println(map);
        List<String> threeHighCalor = menu.stream().filter(dish -> dish.getCalories() > 300).map(Dish::getName).limit(3).collect(Collectors.toList());
        System.out.println(threeHighCalor);
        //流只能遍历一次，被消费一次
        Stream<String> s = threeHighCalor.stream();
        s.forEach(System.out::println);
//        s.forEach(System.out::println);

        List<String> word = Arrays.asList("Hello","World");
       List<String[]>  list0 = word.stream().map(w ->w.split("")).distinct().collect(Collectors.toList());
      // List<String> list1 = word.test.stream().map(w ->w.split("")).map(Arrays::test.stream).distinct().collect(Collectors.toList());
        List<String> list1 = word.stream().map(w ->w.split("")).flatMap(Arrays::stream).distinct().collect(Collectors.toList());
        List<Integer> numList = Arrays.asList(1,2,3,4,5,6);
        System.out.println(numList.stream().map(n ->n*n).collect(Collectors.toList()));
        List<Integer> s1 = Arrays.asList(1,2,3);
        List<Integer> s2 = Arrays.asList(3,4);
        System.out.println( s1.stream().flatMap(i -> s2.stream().map(j -> new int[]{i,j})).collect(toList()));
        boolean flag =  menu.stream().anyMatch(dish -> dish.getName().equals("pork")); //至少一个符合
        boolean flag1 = menu.stream().allMatch(dish -> dish.getCalories()<10000); //全部符合
        boolean flag2 = menu.stream().noneMatch(dish -> dish.getCalories()<10); //全部不符合
        System.out.println(flag);
        System.out.println(flag1);
        System.out.println(flag2);
        menu.stream().findAny().ifPresent(d -> System.out.println(d.getName()));
        Optional<Integer> op =  s1.stream().reduce((a,b)->a*b);
        Integer op1 =  s1.stream().reduce(0,(a,b)->a*b);
        int op2 = s1.stream().reduce(0,Integer::sum);
        System.out.println(op.get());
        System.out.println(op1);
        System.out.println( menu.stream().map(dish -> 1).reduce(Integer::sum));

       Optional<Dish> dish01 =   menu.stream().collect(maxBy(comparing(Dish::getCalories)));
        System.out.println(dish01);
        System.out.println(menu.stream().map(Dish::getName).collect(joining(",")));


    }
}
